package com.liming.salesman.webapp.wicket.page;

import com.liming.core.web.wicket.BasePage;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.AbstractReadOnlyModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @类描述：消息推送
 * @创建人：sunlf
 * @创建时间：2014-03-29 上午10:42
 * @修改人：
 * @修改时间：
 * @修改备注：
 */

public class ListMessage extends BasePage {

    Message formMessage = new Message();

    public ListMessage() {
        List<Message> messageList = AllEntity();
        ListDataProvider<Message> listDataProvider = new ListDataProvider<Message>(messageList);
        add(new DataView<Message>("messageRows", listDataProvider) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final Item<Message> item) {
                final Message message;
                message = item.getModelObject();

                item.add(new Label("receiverData", message.getReceiver()));
                item.add(new Label("contentData", message.getContent()));
                item.add(new Label("createDateData", message.getCreateDate()));
                item.add(AttributeModifier.replace("class", new AbstractReadOnlyModel<String>() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public String getObject() {
                        return (item.getIndex() % 2 == 1) ? "even" : "odd";
                    }
                }));
            }
        });
/*

        Form<Message> form = new Form<Message>("add_message_form", new CompoundPropertyModel<Message>(formMessage));
        add(form);
        AjaxFormValidatingBehavior.addToAllFormComponents(form, "keydown", Duration.ONE_SECOND);

        TextField formReceiver = new TextField("receiver");
        TextField formContent = new TextField("content");
        TextField formCreateDate = new TextField("createDate");

        form.add(formReceiver);
        form.add(formContent);
        form.add(formCreateDate);

        form.add(new AjaxButton("submit_button", form)
        {
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form)
            {
                System.out.println("onSubmit");
                Message message = (Message)getParent().getDefaultModelObject();
                System.out.println(message.getContent());
            }

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form)
            {
                System.out.println("onError");
            }
        });
*/

    }

    private List<Message> AllEntity() {
        List<Message> messageList = new ArrayList<Message>();
        Message message1 = new Message();

        message1.setContent("请更新软件");
        message1.setReceiver("红旗4S店");
        message1.setCreateDate("2013-03-29");
        message1.setSender("厂商管理员");

        messageList.add(message1);
        return messageList;
    }

    class Message implements Serializable {
        String content;
        String receiver;
        String createDate;
        String sender;

        public String getSender() {
            return sender;
        }

        public void setSender(String sender) {
            this.sender = sender;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public String getReceiver() {
            return receiver;
        }

        public void setReceiver(String receiver) {
            this.receiver = receiver;
        }

        public String getCreateDate() {
            return createDate;
        }

        public void setCreateDate(String createDate) {
            this.createDate = createDate;
        }


    }
}
